package com.chookss.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chookss.R;
import com.chookss.home.adapter.PublishAdapter;
import com.chookss.home.entity.NavigationEntity;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.PermissionSpUtils;
import com.chookss.tools.Utils;
import com.chookss.view.listener.HomeBottomDialogListener;

import java.util.ArrayList;

import io.reactivex.annotations.NonNull;


public class UploadBottomDialog extends Dialog {
    private LinearLayout llMiddle, llNew, llDoc;
    private TextView tv_cancel;
    private RelativeLayout rlAll;
    private RecyclerView recyclerView;
    private Context context;
    private int catalogType;
    private int type = 0; //0知识库1资料库
    private PublishAdapter publishAdapter;
    private ArrayList<NavigationEntity> navigationEntityList = new ArrayList<>();
    private HomeBottomDialogListener listener = null;

    public UploadBottomDialog(@NonNull Context context, int type,String catalogType, HomeBottomDialogListener listener) {
        super(context);
        this.context = context;
        this.type = type;
        this.catalogType = Utils.changeNumber(catalogType);
        this.listener = listener;
        initLayoutRes();
    }

    public UploadBottomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initLayoutRes();
    }

    private void initLayoutRes() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_upload_bottom, null);
        rlAll = view.findViewById(R.id.rlAll);
        llMiddle = view.findViewById(R.id.llMiddle);
        llDoc = view.findViewById(R.id.llDoc);
        llNew = view.findViewById(R.id.llNew);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        recyclerView = view.findViewById(R.id.recyclerView);
        if (type == 0) {
            recyclerView.setVisibility(View.VISIBLE);
            //导航
            NavigationEntity navigationEntity = new NavigationEntity();
            navigationEntity.setNavigationName("新建文件夹");
            navigationEntity.setLocalUrl(R.drawable.icon_upload_folder);
            navigationEntityList.add(navigationEntity);
//            （0：全局 1: 题库 2:视频 3:资料库 4：图库 5：图文）
            if (PermissionSpUtils.getBoolean(context,"03",false)&&(catalogType==0||catalogType==3)) {
                NavigationEntity navigationEntity2 = new NavigationEntity();
                navigationEntity2.setNavigationName("上传文档");
                navigationEntity2.setLocalUrl(R.drawable.icon_upload_word);
                navigationEntityList.add(navigationEntity2);
            }
            if (PermissionSpUtils.getBoolean(context,"10",false)&&(catalogType==0||catalogType==2)) {
                NavigationEntity navigationEntity3 = new NavigationEntity();
                navigationEntity3.setNavigationName("上传视频");
                navigationEntity3.setLocalUrl(R.drawable.icon_upload_video);
                navigationEntityList.add(navigationEntity3);
            }
            if (PermissionSpUtils.getBoolean(context,"25",false)&&(catalogType==0||catalogType==4)) {
                NavigationEntity navigationEntity4 = new NavigationEntity();
                navigationEntity4.setNavigationName("上传图片");
                navigationEntity4.setLocalUrl(R.drawable.icon_upload_pic);
                navigationEntityList.add(navigationEntity4);
            }

            if (PermissionSpUtils.getBoolean(context,"16",false)&&(catalogType==0||catalogType==5)) {
                NavigationEntity navigationEntity5 = new NavigationEntity();
                navigationEntity5.setNavigationName("上传图文");
                navigationEntity5.setLocalUrl(R.drawable.icon_upload_tuwen);
                navigationEntityList.add(navigationEntity5);
            }
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
            recyclerView.setLayoutManager(gridLayoutManager);
            publishAdapter = new PublishAdapter(R.layout.item_home_publish, context, navigationEntityList);
            recyclerView.setAdapter(publishAdapter);
            publishAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@androidx.annotation.NonNull BaseQuickAdapter<?, ?> adapter, @androidx.annotation.NonNull View view, int position) {

                    if (listener != null) {
                        if (navigationEntityList.get(position).getNavigationName().equals("新建文件夹")) {
                            listener.onListItemClick(0);
                        } else if (navigationEntityList.get(position).getNavigationName().equals("上传文档")) {
                            listener.onListItemClick(1);
                        } else if (navigationEntityList.get(position).getNavigationName().equals("上传视频")) {
                            listener.onListItemClick(2);
                        } else if (navigationEntityList.get(position).getNavigationName().equals("上传图片")) {
                            listener.onListItemClick(3);
                        } else if (navigationEntityList.get(position).getNavigationName().equals("上传图文")) {
                            listener.onListItemClick(4);
                        }
                        dismiss();
                    }
                }
            });
        } else {
            llNew.setVisibility(View.VISIBLE);
            if (PermissionSpUtils.getBoolean(context,"03",false)&&catalogType==3) {
                llDoc.setVisibility(View.VISIBLE);
            }
            llMiddle.setVisibility(View.VISIBLE);
        }
        llNew.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(0);
            }
            dismiss();
        });
        llDoc.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(1);
            }
            dismiss();
        });
        rlAll.setOnClickListener(v -> {
            dismiss();
        });
        tv_cancel.setOnClickListener(v -> {
            dismiss();
        });
        setContentView(view);
        initBottom();
    }

    protected void initBottom() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        Window wm = getWindow();
        lp.width = lp.MATCH_PARENT;
        lp.height = lp.MATCH_PARENT;
        lp.dimAmount = 0.6f;
        wm.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        wm.setGravity(Gravity.BOTTOM);
        wm.setWindowAnimations(R.style.DialogFullWidthDimNotEnabled);
        getWindow().setAttributes(lp);
    }

    protected void safeDismiss() {
        if (isShowing()) {
            dismiss();
        }
    }
}
